//
//  ST_CompatibilityBarrage.swift
//  BTVideo
//
//  Created by DouLei on 2019/8/22.
//  Copyright © 2019 DouLei. All rights reserved.
//

import UIKit
import SwifterSwift

private var speedPercentRang = 0.6...1.1

func ST_CompatibilityBarrageItem(item:ST_BarrageModel)->ST_FWBarrageItemModel{
    
    
    let str = create_barrage_attributed_string(item: item)
                
    var size = str.size()
    
    var m = ST_FWBarrageItemModel.init(attributedStr: str, size: size)
    
    var avatar = item.avatar
//        没有头像用自己的头像
    if avatar == ""{
        avatar = ST_BTUserInfoManager.shared.current_account_info?.userinfo?.avatar ?? ""
    }
    
    m.userAvatar = avatar
    
    m.backgroundColor = UIColor.init(hexString: item.reply_color) ?? .clear
    
    size = CGSize.init(width: size.width + barrageLabelHeight + 13, height: barrageLabelHeight)

    m.speed = m.speed * Double.random(in: speedPercentRang).cgFloat
    
    m.vip = item.is_vip
    
    m.size = size
    
    return m
}

func is_normal_barrage(item:ST_BarrageModel)->Bool{
    
//    let currentColor = Color.init(hexString: item.reply_color) ?? Color.white
    
//    return currentColor.hexString  == Color.white.hexString
    
    return item.is_vip == false
}

func create_barrage_attributed_string(item:ST_BarrageModel)->NSAttributedString{
    
   
    if !is_normal_barrage(item: item){

        return create_rich_barrage_attributed_string(item: item)
    }
    
    return create_normal_barrage_attributed_string(item: item)
}


private func create_rich_barrage_attributed_string(item:ST_BarrageModel)->NSAttributedString{
    
    
//    let shadow = NSShadow()
//
//    shadow.shadowBlurRadius = 4
//
//    shadow.shadowColor = UIColor.init(white: 0, alpha: 0.6)
//
//    shadow.shadowOffset = CGSize.init(width: 1, height: 1)
    
    var attributes =  [NSAttributedString.Key : Any]()
    
    attributes.updateValue(UIFont.systemFont(ofSize: 16), forKey: .font)
    
    let textColor = Color.white
    
    attributes.updateValue(textColor, forKey: .foregroundColor)
    
//    attributes.updateValue(shadow, forKey: .shadow)
    
    
    let attrString = NSMutableAttributedString(string: item.reply_content)
    
    attrString.addAttributes(attributes, range: NSRange(location: 0, length: attrString.length))
    
    return attrString
}

private func create_normal_barrage_attributed_string(item:ST_BarrageModel)->NSAttributedString{
    
    
    let shadow = NSShadow()
    
    shadow.shadowBlurRadius = 4
    
    shadow.shadowColor = UIColor.init(white: 0, alpha: 0.6)
    
    shadow.shadowOffset = CGSize.init(width: 1, height: 1)
    
    var attributes =  [NSAttributedString.Key : Any]()
    
    attributes.updateValue(UIFont.systemFont(ofSize: 18), forKey: .font)
    
    let textColor = Color.white
    
    attributes.updateValue(textColor, forKey: .foregroundColor)
    
    attributes.updateValue(shadow, forKey: .shadow)
    
    
    let attrString = NSMutableAttributedString(string: item.reply_content)
    
    attrString.addAttributes(attributes, range: NSRange(location: 0, length: attrString.length))
    
    return attrString
}
